package cn.boxiao.bxn.uic.rule.participant.department;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import cn.boxiao.bxn.uic.model.Organization;
import cn.boxiao.bxn.uic.model.User;
import cn.boxiao.bxn.uic.rule.Rulable;
import cn.boxiao.bxn.uic.rule.participant.Participant;
import cn.boxiao.bxn.uic.rule.participant.Participant.ParticipantType;
import cn.boxiao.bxn.uic.service.OrganizationService;
import cn.boxiao.bxn.uic.service.UserQueryService;

import com.google.common.collect.Lists;
/**
 * 根据用户分类取得对应的用户分类项，例如如果是教师，则返回：全体教师、全体班主任、全体教研组长、..
 * 如果是学生，则返回：全体学生、全体高一年级、。。。 * 
 * 
 * @author Yunsheng
 *
 */
public class GetUserClustersRule implements Rulable<Participant> {
	private Logger logger = LoggerFactory.getLogger(getClass());

	private final static String RULE_PREFIX = "getUserClustersRule-";
	private final static String CLUSTER_CLZ_MASTER = "clzMaster"; //班主任
	private final static String CLUSTER_GRD_MASTER = "grdMaster"; //年级组长
	private final static String CLUSTER_CSE_MASTER = "courseMaster"; //备课组长
	
	@Autowired
    private UserQueryService userService;
	
	@Autowired
	private OrganizationService orgService;
    
	@Override
	public List<Participant> run(String userCategory) {
		List<Participant> participants = Lists.newArrayList();
		if (userCategory.equals(String.valueOf(User.CATEGORY_TEACHER))) {
			participants.add(this.createClzMaster());
			participants.add(this.createGrdMaster());
//			participants.add(this.createCourseMaster());
		} else if (userCategory.equals(String.valueOf(User.CATEGORY_STUDENT))) {
			//TODO:not implemented yet.
		}
		return participants;
	}

	@Override
	public List<Participant> echo(List<String> params) {
		List<Participant> participants = Lists.newArrayList();
		for (String param:params) {
			if (param.equals(CLUSTER_CLZ_MASTER)) {
				participants.add(this.createClzMaster());
			} else if (param.equals(CLUSTER_GRD_MASTER)) {
				participants.add(this.createGrdMaster());
			} else if (param.equals(CLUSTER_CSE_MASTER)) {
				participants.add(this.createCourseMaster());
			}
		}
		return participants;
	}
	
	@Override
	public List<User> echoUsers(List<String> params) {
		logger.debug("######params#######:" + params.toString());
		
		List<User> users = Lists.newArrayList();
		for (String param:params) {
			if (param.equals(CLUSTER_CLZ_MASTER)) {
				users.addAll(this.getClzMasterUsers());
			} else if (param.equals(CLUSTER_GRD_MASTER)) {
				users.addAll(this.getGrdMasterUsers());
			} else if (param.equals(CLUSTER_CSE_MASTER)) {
				users.addAll(this.getCourseMasterUsers());
			}
		}
		
		return users;
	}
	
	private List<User> getClzMasterUsers() {
		
		def f = new File("c:/temp/temp.log")
		f << "######getClzMasterUsers#######\r\n"

		List<User> users = Lists.newArrayList();
		try {
			List<Organization> clzs = orgService.findOrganizationsByType(Organization.ORG_TYPE_CLZ);
			if (!CollectionUtils.isEmpty(clzs)) {
				for (Organization clz:clzs) {
					users.addAll(userService.findUsersByPosition(clz.getId(), User.POSITION_CLZ_MASTER));
				}
			}
		} catch (Exception e) {
			logger.warn("Failed to get class master", e);
		}
		logger.debug("######getClzMasterUsers:" + users.toString());
		
		return users;
	}
	
	private List<User> getGrdMasterUsers() {
		logger.debug("######getGrdMasterUsers#######");
		
		List<User> users = Lists.newArrayList();
		try {
			List<Organization> clzs = orgService.findOrganizationsByType(Organization.ORG_TYPE_GRADE);
			if (!CollectionUtils.isEmpty(clzs)) {
				for (Organization clz:clzs) {
					users.addAll(userService.findUsersByPosition(clz.getId(), User.POSITION_GRADE_MASTER));
				}
			}
		} catch (Exception e) {
			logger.warn("Failed to get grade master", e);
		}
		logger.debug("######getGrdMasterUsers:" + users.toString());
		
		return users;
	}
	
	private List<User> getCourseMasterUsers() {
		return Lists.newArrayList();//TODO:not implemented yet, wait for tic...
	}
	
	private Participant createClzMaster() {
		Participant participant = new Participant();
		participant.setParam(RULE_PREFIX + CLUSTER_CLZ_MASTER);
		participant.setLabel("全体班主任");
		participant.setActive(true);
		participant.setTarget(true);
		participant.setNextRuleId(null);
		participant.setType(ParticipantType.DYNAMIC);
		return participant;
	}
	
	private Participant createGrdMaster() {
		Participant participant = new Participant();
		participant.setParam(RULE_PREFIX + CLUSTER_GRD_MASTER);
		participant.setLabel("全体年级组长");
		participant.setActive(true);
		participant.setTarget(true);
		participant.setNextRuleId(null);
		participant.setType(ParticipantType.DYNAMIC);
		return participant;
	}
	
	private Participant createCourseMaster() {
		Participant participant = new Participant();
		participant.setParam(RULE_PREFIX + CLUSTER_CSE_MASTER);
		participant.setLabel("全体备课组长");
		participant.setActive(true);
		participant.setTarget(true);
		participant.setNextRuleId(null);
		participant.setType(ParticipantType.DYNAMIC);
		return participant;
	}

}
